package com.gxuwz.bignews.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.gxuwz.bignews.R;
import com.gxuwz.bignews.controller.ForgotPasswordController;
import com.gxuwz.bignews.model.util.NotificationUtil;

import java.util.Random;

public class ForgotPasswordFragment extends Fragment {
    private EditText etPhoneRecover, etVerificationCode, etNewPassword, etConfirmPassword;
    private Button btnGetVerificationCode, btnVerifyCode, btnResetPassword, btnBackToLogin;
    private View layoutStep1, layoutStep2, layoutStep3;
    private ForgotPasswordController forgotPasswordController;
    private String verificationCode;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_forgot_password, container, false);

        forgotPasswordController = new ForgotPasswordController(getContext());

        // 初始化视图
        etPhoneRecover = view.findViewById(R.id.et_phone_recover);
        etVerificationCode = view.findViewById(R.id.et_verification_code);
        etNewPassword = view.findViewById(R.id.et_new_password);
        etConfirmPassword = view.findViewById(R.id.et_confirm_password);

        btnGetVerificationCode = view.findViewById(R.id.btn_get_verification_code);
        btnVerifyCode = view.findViewById(R.id.btn_verify_code);
        btnResetPassword = view.findViewById(R.id.btn_reset_password);
        btnBackToLogin = view.findViewById(R.id.btn_back_to_login);

        layoutStep1 = view.findViewById(R.id.layout_step_1);
        layoutStep2 = view.findViewById(R.id.layout_step_2);
        layoutStep3 = view.findViewById(R.id.layout_step_3);

        // 设置初始状态
        layoutStep1.setVisibility(View.VISIBLE);
        layoutStep2.setVisibility(View.GONE);
        layoutStep3.setVisibility(View.GONE);

        // 设置点击事件
        btnGetVerificationCode.setOnClickListener(v -> getVerificationCode());
        btnVerifyCode.setOnClickListener(v -> verifyCode());
        btnResetPassword.setOnClickListener(v -> resetPassword());
        btnBackToLogin.setOnClickListener(v -> goBackToLogin());

        return view;
    }

    private void getVerificationCode() {
        String phone = etPhoneRecover.getText().toString().trim();

        if (phone.isEmpty()) {
            Toast.makeText(getContext(), "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }

        // 检查手机号是否存在
        if (forgotPasswordController.isPhoneRegistered(phone)) {
            // 生成随机6位验证码
            verificationCode = generateRandomCode();
            
            // 显示通知栏验证码
            if (getContext() != null) {
                NotificationUtil.showVerificationCodeNotification(getContext(), verificationCode);
                Toast.makeText(getContext(), "验证码已发送到通知栏", Toast.LENGTH_SHORT).show();
            }

            // 显示下一步
            layoutStep1.setVisibility(View.GONE);
            layoutStep2.setVisibility(View.VISIBLE);
        } else {
            Toast.makeText(getContext(), "该手机号未注册", Toast.LENGTH_SHORT).show();
        }
    }
    
    // 生成6位随机验证码
    private String generateRandomCode() {
        Random random = new Random();
        int code = 100000 + random.nextInt(900000); // 生成100000-999999之间的随机数
        return String.valueOf(code);
    }

    private void verifyCode() {
        String code = etVerificationCode.getText().toString().trim();

        if (code.isEmpty()) {
            Toast.makeText(getContext(), "请输入验证码", Toast.LENGTH_SHORT).show();
            return;
        }

        // 验证验证码
        if (code.equals(verificationCode)) {
            Toast.makeText(getContext(), "验证码正确", Toast.LENGTH_SHORT).show();

            // 显示下一步
            layoutStep2.setVisibility(View.GONE);
            layoutStep3.setVisibility(View.VISIBLE);
        } else {
            Toast.makeText(getContext(), "验证码错误", Toast.LENGTH_SHORT).show();
        }
    }

    private void resetPassword() {
        String newPassword = etNewPassword.getText().toString().trim();
        String confirmPassword = etConfirmPassword.getText().toString().trim();
        String phone = etPhoneRecover.getText().toString().trim();

        if (newPassword.isEmpty() || confirmPassword.isEmpty()) {
            Toast.makeText(getContext(), "请填写完整信息", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!newPassword.equals(confirmPassword)) {
            Toast.makeText(getContext(), "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }

        // 更新密码
        boolean result = forgotPasswordController.resetPassword(phone, newPassword);
        if (result) {
            Toast.makeText(getContext(), "密码重置成功", Toast.LENGTH_SHORT).show();
            goBackToLogin();
        } else {
            Toast.makeText(getContext(), "密码重置失败", Toast.LENGTH_SHORT).show();
        }
    }

    private void goBackToLogin() {
        if (getActivity() != null) {
            getActivity().getSupportFragmentManager().popBackStack();
        }
    }
}